import Taro from '@tarojs/taro';
import { View,Text,Image } from '@tarojs/components';
import './index.scss'
import http from '@/utils/http';

export default class MessageDetails extends Taro.Component {
    constructor() {
        super();
        this.state = {
            noticeDetail: {}
        }
    }
    componentWillMount(){
        this.getNoticeDetail();
    }
    config = {
        navigationBarTitleText: '消息中心'
    }
    getNoticeDetail(){
        http.get('getNoticeDetail',{
            id: this.$router.params.id
        }).then((res)=>{
            res.datas.timesString = this.timesFun(res.datas.createtime.replace(/-/g, "/"));
            this.setState({
                noticeDetail: res.datas
            })
        }).catch((err)=>{
            console.log(err);
        })
    }
    deleteNotice() {
        Taro.showModal({
            title: '温馨提示',
            content: '确定删除该信息',
            confirmText: '确定',
            cancelText: '取消',
            success: (res) => {
                if (res.confirm) {
                    http.post('deleteNotice', {
                        id: this.$router.params.id
                    }, false, true, false).then(() => {
                        Taro.navigateBack();
                    }).catch((err) => {
                        console.log(err);
                    })
                }
            }
        })
    }
    timesFun(timesData) {
        var dateBegin = new Date(timesData);
        var dateEnd = new Date(); //获取当前时间
        var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
        var leave1 = dateDiff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
        var timesString = '';

        if (dayDiff != 0) {
            timesString = timesData.substring(0, timesData.length - 3);
        } else if (dayDiff == 0 && hours != 0) {
            var current_times = timesData.split(' ')[1].substring(0, 5);
            timesString = (dateEnd.getHours() < hours ? '昨天' : '今天') + current_times;
        } else if (dayDiff == 0 && hours == 0) {
            timesString = '刚刚';
            if (minutes) {
                timesString = minutes + '分钟之前';
            }
        }
        return timesString
    }
    render() {
        const { noticeDetail } = this.state;
        return (
            <View className='message-details box_s pl-2-5 pr-2-5 ov_h'>
                <View className='mt-2-5 flex flex-c  pl-4 pr-4 pt-3 pb-6-5 bdr_10 bg_fff'>
                    <View className='flex flex-r flex-item-between'>
                        <View className='ft28 fc_53C'><Text className={noticeDetail.content_type === '1' ? 'fc_FF8' : 'fc_960'}>{noticeDetail.content_type === '1' ? '提醒' :'公告' }：</Text>{noticeDetail.title || '-'}</View>
                        <Image className='icon-del' onClick={() => { this.deleteNotice() }} src={require('../../../assets/imgs/sigin/ic-del.png')}></Image>
                    </View>
                    <Text className='ft24 fc_B9C pt-2-5'>时间：{noticeDetail.timesString}</Text>
                    <Text className='ft26 fc_65C pt-4-5'>{noticeDetail.content}</Text>
                    <Text className='ft24 fc_65C pt-6-5 text-align'>部门：{noticeDetail.department_name || '-'}</Text>
                    <Text className='ft24 fc_B9C pt-2 text-align'>时间：{noticeDetail.createtime}</Text>
                </View>
            </View>
        )
    }
}

